﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Linq.Mapping;
using System.Data.Linq.Provider;
using System.Data.Linq.SqlClient;

namespace WinForms
{
    public partial class Form_Linq : Form
    {
        public Form_Linq()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LinqProductDataContext dc = new LinqProductDataContext();
            dc.tblproduct.Where(n => n.productname == "dd");

            dc.aspnet_Users.Where(n => n.LoweredUserName == "").Select
                (
                m => new
                {
                    m.LoweredUserName,
                    m.IsAnonymous,
                    //_________LEFT JOIN 连接查询__
                    role = dc.aspnet_Roles.FirstOrDefault(x => x.RoleName == m.UserName).RoleName
                }
                );

            var xproduct = dc.tblproduct.FirstOrDefault(p=>p.productname=="xxx");
            xproduct.productname = "yyyy";
            try
            {
                dc.SubmitChanges();
            }
            catch ( Exception ee)
            {

                //foreach (ObjectChangeConflict confict in dc.ChangeConflicts)
                //{
                //    confict.Resolve(RefreshMode.OverwriteCurrentValues);
                //}

//这里RefreshMode就是表示解决冲突的方法，也有三种：

//KeepChanges 把改变过的属性值更新到数据库，没有改变过的属性值就用数据库的当前值

//KeepCurrentValues 把当前所有值更新到数据库

//OverwriteCurrentValues 使用数据库的当前值，不做强行更新
            }
            finally
            {
                dc.SubmitChanges();
            }
            
        }
    }
}
